package mobile.persistencia;

import mobile.dominio.*;
import moio.util.ArrayList;
import waba.io.Catalog;
import waba.io.DataStream;
import waba.io.IOError;
import waba.io.ResizeRecord;
import waba.sys.Settings;

public class PProducto extends Catalog {
	private ResizeRecord rs;
	private DataStream ds;

	public static PProducto instance = new PProducto();

	public PProducto(){
		super("Producto.mGqv.DATA", Catalog.CREATE);
		//super("Producto."+ Settings.appCreatorId +".DATA", Catalog.CREATE);
		rs = new ResizeRecord(this, 128);
		ds = new DataStream(rs);
	}

	public void write(Producto unProducto, boolean update){
		try {
			if (update)
			{
				int n = indexOf(unProducto);
				if (n == -1 || !rs.restartRecord(n)) 
					return;
			}
			else
				rs.startRecord(); /* start the record at the next available position */

			unProducto.saveTo(ds);
			rs.endRecord(); /* finishes the writing of a record */
		} catch (IOError e) {}
	}

	/**
	 * Removes a record ( Customer ) from the pdb file
	 * 
	 * @param cli
	 *           the Customer
	 */
	public void remove(Producto unProducto)
	{
		/*
		 * We must set the record position to the record we want to delete, and then delete
		 * the record.
		 */
		int n = indexOf(unProducto);
		try {
			if (n >= 0 && this.setRecordPos(n)){
				this.deleteRecord();
			}
		}catch (IOError e) {}

	}

	/**
	 * Find the index ( position ) of the record ( Customer ) inside the pdb file
	 * 
	 * @param cli
	 *           the Customer
	 * @return the index position or -1 if the Customer was not found on the record
	 */
	public int indexOf(Producto unProducto)
	{
		for (int n = this.getRecordCount() - 1; n >= 0; n--)
			try {
				if (this.setRecordPos(n)) {
					Producto elProducto = read();

					if (elProducto.getId() == unProducto.getId()) {
						return n;
					}
				}
			} catch (IOError e) {

			}
			return -1;
	}

	/**
	 * Reads a record ( Customer ) and construct a Customer. Notice that the order in which
	 * you read informations from the Customer is relevant.
	 * 
	 * @return the Customer
	 */
	public Producto read()
	{
		return new Producto(ds);
	}

	/**
	 * Returns a specific record ( Customer )
	 * 
	 * @param name
	 *           Name of the Customer
	 * @return The Customer or null if the Customer was not found
	 */
	public Producto obtenerProductoXID(int id){
		for (int n = this.getRecordCount() - 1; n >= 0; n--){
			try {
				if (this.setRecordPos(n)){
					Producto unProducto = read();
					if (id == unProducto.getId()){
						return unProducto;
					}
				}
			} catch(IOError e){
			}
		}
		return null;
	}
	
	public ArrayList todosLosProductos(){
		ArrayList colProductos = new ArrayList();
		for (int n = this.getRecordCount() - 1; n >= 0; n--){
			try {
				if (this.setRecordPos(n)){
					Producto unProducto = read();
					colProductos.add(unProducto);
				}
			} catch(IOError e){
			}
		}
		return colProductos;
	}
}
